const express = require("express");
const Result = require("../../models/Result");
const { decoded } = require("../../utils/index");
const { PRIVATE_KEY, JWT_EXPIRED } = require("../../utils/constant");
const {
  getRoomList,
  editRoom,
  createRoom,
  deleteRoom,
  getTotalCount,
  getRoomSelectData,
  getFreeClassroomList,
} = require("../../services/classroom");
const jwt = require("jsonwebtoken");
const jwtAuth = require("../jwt");
// 注册路由
const router = express.Router();
router.use(jwtAuth);

//获取教室列表接口
router.get("/list", function (req, res, next) {
  const { pageNum, pageSize } = req.query;
  getRoomList(req.query)
    .then((data) => {
      data.forEach((item) => {
        item.space = JSON.parse(item.space);
      });
      getTotalCount(req.query).then((total) => {
        new Result(
          {
            list: data,
            page: {
              pageNum,
              pageSize,
              total: total.totalCount,
            },
          },
          "教室列表查询成功"
        ).success(res);
      });
    })
    .catch((err) => {
      new Result("教室信息查询失败").fail(res);
    });
});

router.get("/getFreeClassroomList", async function (req, res, next) {
  const { sectionId, weekNum } = req.query;
  console.log("传入参数：", sectionId, weekNum);
  const data = await getFreeClassroomList(sectionId);
  const _data = [];
  for (let i = 0; i < data.length; i++) {
    if (data[i].roomStatus === 1) {
      continue;
    }

    if (
      !data[i].weekNumArr ||
      data[i].weekNumArr === null ||
      data[i].weekNumArr === undefined
    ) {
      _data.push(data[i]);
      continue;
    }
    if (
      !(
        (JSON.parse(data[i].weekNumArr).includes(weekNum) ||
          JSON.parse(data[i].weekNumArr).includes(Number(weekNum))) &&
        data[i].linkSectionId == sectionId
      )
    ) {
      console.log(
        JSON.parse(data[i].weekNumArr),
        weekNum,
        JSON.parse(data[i].weekNumArr).includes(Number(weekNum))
      );
      _data.push(data[i]);
    }
  }
  new Result(_data, "教室列表查询成功").success(res);
});

router.get("/getRoomSelectData", function (req, res, next) {
  const { searchContent } = req.query;
  if (
    searchContent === undefined ||
    searchContent === null ||
    searchContent.trim() === ""
  ) {
    new Result([], "教室列表查询成功").success(res);
  } else {
    getRoomSelectData(searchContent)
      .then((data) => {
        data.forEach((item) => {
          item.label = item.roomCode + " " + item.roomName;
          item.value = item.id;
          item.space = JSON.parse(item.space);
          delete item.roomCode;
          delete item.roomName;
          delete item.id;
        });
        new Result(data, "教室列表查询成功").success(res);
      })
      .catch((err) => {
        new Result("教室信息查询失败").fail(res);
      });
  }
});

//编辑教室接口
router.post("/edit", function (req, res, next) {
  editRoom(req.body)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      new Result("操作失败").fail(res);
    });
});

//创建教室接口
router.post("/create", function (req, res, next) {
  createRoom(req.body)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      new Result("操作失败").fail(res);
    });
});

//删除教室接口
router.post("/delete", function (req, res, next) {
  deleteRoom(req.body)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      new Result("操作失败").fail(res);
    });
});

module.exports = router;
